{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import os\n",
    "from nltk.parse import stanford"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分词结果:  2022 年 的 北京 东奥会 是 第 24 届 冬季 奥林匹克运动会\n"
     ]
    }
   ],
   "source": [
    "string = '2022年的北京东奥会是第24届冬季奥林匹克运动会'\n",
    "seg_list = jieba.cut(string, cut_all = False, HMM = True)\n",
    "seg_str = ' '.join(seg_list)\n",
    "print('分词结果: ', seg_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.environ.get('JAVA_HOME'):\n",
    "    JAVA_HOME = 'C:\\Program Files\\Java\\jdk-21'\n",
    "    os.environ['JAVA_HOME'] = JAVA_HOME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:9: DeprecationWarning: The StanfordParser will be deprecated\n",
      "Please use \u001b[91mnltk.parse.corenlp.StanforCoreNLPParser\u001b[0m instead.\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "#Stanford Parser的jar文件路径\n",
    "parser_path = 'stanford-parser.jar'\n",
    "#Stanford Parser模型的jar文件路径\n",
    "model_path = 'stanford-parser-4.2.0-models.jar'\n",
    "#中文句法分析模型\n",
    "pcfg_path='edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz'\n",
    "#实例化StanfordParser类的对象\n",
    "parser = stanford.StanfordParser(path_to_jar = parser_path,\n",
    "                                path_to_models_jar = model_path, model_path = pcfg_path)\n",
    "sentence = parser.raw_parse(seg_str)   #对分词后的句子进行句法分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "句法分析的结果: \n",
      " (ROOT\n",
      "  (IP\n",
      "    (NP\n",
      "      (DNP (QP (CD 2022) (CLP (M 年))) (DEG 的))\n",
      "      (NP (NR 北京) (NN 东奥会)))\n",
      "    (VP\n",
      "      (VC 是)\n",
      "      (NP\n",
      "        (QP (OD 第))\n",
      "        (QP (CD 24) (CLP (M 届)))\n",
      "        (NP (NN 冬季) (NN 奥林匹克运动会))))))\n",
      "句法树的叶子节点: \n",
      " ['2022', '年', '的', '北京', '东奥会', '是', '第', '24', '届', '冬季', '奥林匹克运动会']\n"
     ]
    }
   ],
   "source": [
    "for line in sentence:\n",
    "    print('句法分析的结果: \\n', line)\n",
    "    print('句法树的叶子节点: \\n', line.leaves())\n",
    "    line.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
